package com.ruoyi.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.zzw.BO.SafenessRankingBO;
import com.ruoyi.common.core.domain.zzw.TRegionInfo;
import com.ruoyi.common.core.domain.zzw.VO.SafenessRankingVO;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TRegionInfoMapper;
import com.ruoyi.system.service.ITRegionInfoService;

import javax.annotation.Resource;

/**
 * 地区Service业务层处理
 *
 * @author xbx
 * @date 2023-10-20
 */
@Service
public class TRegionInfoServiceImpl extends ServiceImpl<TRegionInfoMapper, TRegionInfo> implements ITRegionInfoService {
    @Resource
    private TRegionInfoMapper tRegionInfoMapper;

    /**
     * 查询地区
     *
     * @param cRegionId 地区主键
     * @return 地区
     */
    @Override
    public TRegionInfo selectTRegionInfoByCRegionId(Long cRegionId) {
        return tRegionInfoMapper.selectTRegionInfoByCRegionId(cRegionId);
    }

    /**
     * 查询地区列表
     *
     * @param tRegionInfo 地区
     * @return 地区
     */
    @Override
    public List<TRegionInfo> selectTRegionInfoList(TRegionInfo tRegionInfo) {
        return tRegionInfoMapper.selectTRegionInfoList(tRegionInfo);
    }

    /**
     * 新增地区
     *
     * @param tRegionInfo 地区
     * @return 结果
     */
    @Override
    public int insertTRegionInfo(TRegionInfo tRegionInfo) {
        return tRegionInfoMapper.insertTRegionInfo(tRegionInfo);
    }

    /**
     * 修改地区
     *
     * @param tRegionInfo 地区
     * @return 结果
     */
    @Override
    public int updateTRegionInfo(TRegionInfo tRegionInfo) {
        return tRegionInfoMapper.updateTRegionInfo(tRegionInfo);
    }

    /**
     * 批量删除地区
     *
     * @param cRegionIds 需要删除的地区主键
     * @return 结果
     */
    @Override
    public int deleteTRegionInfoByCRegionIds(List<Long> cRegionIds) {
        return tRegionInfoMapper.deleteTRegionInfoByCRegionIds(cRegionIds);
    }

    /**
     * 删除地区信息
     *
     * @param cRegionId 地区主键
     * @return 结果
     */
    @Override
    public int deleteTRegionInfoByCRegionId(Long cRegionId) {
        return tRegionInfoMapper.deleteTRegionInfoByCRegionId(cRegionId);
    }

    @Override
    public List<SafenessRankingVO> listSafenessScoreTop10(SafenessRankingBO safenessRankingBO) {

        if (safenessRankingBO!=null && StringUtils.isEmpty(safenessRankingBO.getcYear())){
            safenessRankingBO.setcMonth("10");
            safenessRankingBO.setcYear("2023");
        }
        if (safenessRankingBO!=null && StringUtils.isEmpty(safenessRankingBO.getcMonth())){
            safenessRankingBO.setPreviousYear(String.valueOf(Integer.valueOf(safenessRankingBO.getcYear())-1));
        }else if (safenessRankingBO!=null && StringUtils.isNotEmpty(safenessRankingBO.getcMonth())){
            safenessRankingBO.setcMonth(String.valueOf(Integer.valueOf(safenessRankingBO.getcMonth())));
            safenessRankingBO.setLastMonth(String.valueOf(Integer.valueOf(safenessRankingBO.getcMonth())-1));
            safenessRankingBO.setPreviousYear(safenessRankingBO.getcYear());
        }
        return tRegionInfoMapper.SafenessRanking(safenessRankingBO);
    }

    @Override
    public List<TRegionInfo> listByParent(String districtId) {
        QueryWrapper<TRegionInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(TRegionInfo::getCFatherRegionId, districtId)
        ;
        return this.list(queryWrapper);
    }

    @Override
    public TRegionInfo getByAreaCode(String areaCode) {
        QueryWrapper<TRegionInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(TRegionInfo::getCAreaCode, areaCode)
                .eq(TRegionInfo::getCIsDelete, 1)
        ;
        return this.getOne(queryWrapper);
    }

}
